home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 16840 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: halon.vggas.com!news
  2. From: JYoungman@vggas.com (James Youngman)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Array Summary Algorithm
  5. Date: 12 Apr 1996 11:10:17 GMT
  6. Organization: VG Gas Analysis Systems
  7. Message-ID: <4kldmp$5m8@halon.vggas.com>
  8. References: <4kej9q$fhf$1@mhadg.production.compuserve.com>
  9. NNTP-Posting-Host: 132.147.163.4
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=US-ASCII
  12. X-Newsreader: WinVN 0.99.7
  13.  
  14. In article <4kej9q$fhf$1@mhadg.production.compuserve.com>, 
  15. 75226.1623@CompuServe.COM says...
  16. >
  17. >Has anyone ever heard of, or seen an algorithm that can summarize 
  18. >the contents of an array.  What I mean is this... if an array 
  19. >looks like this:
  20. >
  21. >New York                10
  22. >Delaware                12
  23. >New York                15
  24. >Indiana                 12
  25. >Delaware                10
  26. >
  27. >Create a summary array from the preceeding to look like this:
  28. >New York                25
  29. >Delaware                22
  30. >Indiana                 12
  31. >
  32.  
  33. This looks to me like an aplication for a sort followed by
  34. some trivial code.  You could use qsort() for the sort, if
  35. you wanted.  Also, I seem to remember that Jon Bentley covered
  36. this in "More Programming Pearls", ISBN 0-201-11889-0 (this is
  37. a book I strongly reccommend, along with its predecessor,
  38. "Programming Pearls", ISBN 0-201-10331-1.
  39.  
  40. Here's his code for this (page 17):-
  41.  
  42. #!/usr/bin/awk
  43. { count[$1] = count[$1] + $2 }
  44. END { for (i in count) print i, count[i] }
  45.  
  46. Something very similar is just as trivial in Perl.
  47.  
  48. James Youngman
  49. VG Gas Analysis Systems
  50.  
  51.  
  52.  
  53.